package com.rui.controller;

import com.rui.entity.Student;
import com.rui.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/4/21.
 */
@Controller
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @RequestMapping("/add")
    public String add(Student student){
        studentService.add(student);
        return "student/list";
    }

    @RequestMapping("/check")
    public boolean check(String userName,Integer id) {
        List<Student> students = studentService.findStudents(userName, id);
        boolean pass;
        if (students.size() > 0) {
            pass = false;
        } else {
            pass = true;
        }
        return pass;
    }

    @RequestMapping("/delete")
    public String delete(Integer id){
        Student student = studentService.findOne(id);
        studentService.delete(student.getId());
        return "student/list";
    }

    @RequestMapping("/detail")
    public String detail(Integer id){
        Student student = studentService.findOne(id);
        return "student/edit";
    }

    @RequestMapping("/information")
    public String findByName(HttpSession session){
        Student student = (Student) session.getAttribute("keyTwo");
        return "student/information";
    }

    @RequestMapping("/list")
    public String index(String realName,Integer pageSize,Integer pageIndex) {
        List<Student> students = studentService.findStudents(realName, pageSize, pageIndex);
        Integer totalPage = studentService.findTotalPage(realName, pageSize);
        return "student/list";
    }

    @RequestMapping("/update")
    public String update(Student student){
        studentService.update(student);
        return "student/list";
    }

    @RequestMapping("/toAdd")
    public String toAdd(){
        return "student/add";
    }
}
